﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace MailSender
{
    public partial class DefaultFrom : Form
    {
        public DefaultFrom()
        {
            InitializeComponent();
        }

        private void DefaultOkBt_Click(object sender, EventArgs e)
        {
            string fromadd = DefaultFromAddressTxt.Text;
            if (string.IsNullOrEmpty(fromadd))
            {
                MessageBox.Show("发件人不能为空");
                return;
            }
            string host = DefaultHostTxt.Text;
            if (string.IsNullOrEmpty(host))
            {
                MessageBox.Show("主机不能为空");
                return;
            }
            string port = DefaultPortTxt.Text;
            if (string.IsNullOrEmpty(port))
            {
                MessageBox.Show("端口不能为空");
                return;
            }

            string username = UserNameTxt.Text;//用户名
            string password = PasswordTxt.Text;
            //Application.StartupPath("\\DefaultHost.xml");
            bool flag = true;
            try
            {
                XDocument doc = XDocument.Load(SysConstants.DefaultHostFile);
                XElement config = doc.Element("config");
                config.SetElementValue("from",fromadd);
                config.SetElementValue("host",host);
                config.SetElementValue("port", port);
                config.SetElementValue("username", username);
                config.SetElementValue("password", password);
                doc.Save("./DefaultHost.xml");
            }
            catch (Exception)
            {
                flag = false;
                throw;
            }
            MessageBox.Show(flag==true?"保存成功":"保存失败");
            //doc.Save();
            this.Close();
        }

        private void DefaultFrom_Load(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load(SysConstants.DefaultHostFile);
            XElement config = doc.Element("config");
            if (!config.Element("from").IsEmpty)
            {
                DefaultFromAddressTxt.Text = config.Element("from").Value;
            }
            if (!config.Element("host").IsEmpty)
            {
                DefaultHostTxt.Text = config.Element("host").Value;
            }
            if (!config.Element("port").IsEmpty)
            {
                DefaultPortTxt.Text = config.Element("port").Value;
            }
            if (!config.Element("username").IsEmpty)
            {
                UserNameTxt.Text = config.Element("username").Value;
            }
            if (!config.Element("password").IsEmpty)
            {
                PasswordTxt.Text = config.Element("password").Value;
            }
            
        }

        private void DefaultCancelBt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
